package com.ai.smlcore.util;

import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class HiThreeDes
{
  private static final String Algorithm = "DESede";

  public static String encryptMode(String skey, String sdata)
  {
    byte[] key;
    try
    {
      key = new BASE64Decoder().decodeBuffer(skey);
      byte[] data = sdata.getBytes("UTF-8");

      Key deskey = null;

      DESedeKeySpec spec = new DESedeKeySpec(key);
      SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(Algorithm);

      deskey = keyfactory.generateSecret(spec);

      Cipher cipher = Cipher.getInstance("desede/ECB/PKCS5Padding");

      cipher.init(1, deskey);
      byte[] bOut = cipher.doFinal(data);
      return new BASE64Encoder().encode(bOut);
    }
    catch (NoSuchAlgorithmException e1) {
      e1.printStackTrace();
      return null;
    } catch (NoSuchPaddingException e2) {
      e2.printStackTrace();
      return null;
    } catch (Exception e3) {
      e3.printStackTrace();

      return null;
    }
  }

  public static String decryptMode(String skey, String sdata){
    byte[] key;
    try {
      key = new BASE64Decoder().decodeBuffer(skey);
      byte[] data = new BASE64Decoder().decodeBuffer(sdata);

      Key deskey = null;

      DESedeKeySpec spec = new DESedeKeySpec(key);
      SecretKeyFactory keyfactory = SecretKeyFactory.getInstance(Algorithm);

      deskey = keyfactory.generateSecret(spec);
      Cipher cipher = Cipher.getInstance("desede/ECB/PKCS5Padding");
      cipher.init(2, deskey);
      byte[] bOut = cipher.doFinal(data);
      return new String(bOut, "UTF-8");
    }
    catch (NoSuchAlgorithmException e1) {
      e1.printStackTrace();
      return null;
    } catch (NoSuchPaddingException e2) {
      e2.printStackTrace();
      return null;
    } catch (Exception e3) {
      e3.printStackTrace();

      return null;
    }
    
  }

  public static void main(String[] args) {
    String skey = "9853hjutdtgdkypmh79iopkh0hyrdklop934wcnbdyuw41asdcvn85621nmbckm";
    String szSrc = "This is a 3DES test1111111111111111111111111111111111";
    System.out.println("加密前数据:" + szSrc);
    String encoded = encryptMode(skey, szSrc);
    System.out.println("加密后数据" + encoded);
    String decoded = decryptMode(skey, encoded);
    System.out.println("解密后数据:" + decoded);
    
    
    String skey1 = "234hjutdtgdkypmh79iopkh0hyrdklop934wcnbdyuw41asdcvn85621nmbcbn";
    String szSrc1 = "This is a 3DES test1111111111111111111111111111111111";
    System.out.println("加密前数据1:" + szSrc1);
    String encoded1 = encryptMode(skey1, szSrc1);
    System.out.println("加密后数据1" + encoded1);
    String decoded1 = decryptMode(skey1, encoded1);
    System.out.println("解密后数据1:" + decoded1);
    String testStr="XXXfffff00000";
   System.out.println(testStr.substring(testStr.length()-5, testStr.length()));
  }
}